package com.viphuli.app.tool.handler;

import android.content.DialogInterface;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.adapter.ArrangeTypeAdapter;
import com.viphuli.app.tool.bean.page.ArrangeTypeListPage;
import com.viphuli.app.tool.bean.part.ArrangeType;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.fragment.ArrangeTypeEditFragment;
import com.viphuli.app.tool.fragment.ArrangeTypeListFragment;
import com.viphuli.app.tool.http.ApiRequest;

public class ArrangeTypeListResponseHandler extends MyBaseHttpResponseHandler<ArrangeTypeListFragment, ArrangeTypeListPage> implements
		OnItemClickListener, OnItemLongClickListener {

	@Override
	public void deal() {

		if (caller.getList() != null && !caller.getList().isEmpty()) {
			caller.getList().clear();
		}

		caller.getList().addAll(page.getArrangeTypeList());
		BaseAdapter adapter = new ArrangeTypeAdapter(caller.getList());
		caller.setAdapter(adapter);
		caller.getListview().setAdapter(adapter);
		caller.getListview().setOnItemClickListener(this);
		caller.getListview().setOnItemLongClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(caller.getActivity());

		if (tokenKeeper.isLogin()) {

			ArrangeType arrangeType = caller.getList().get(position);
			
			//type 1：班次，3：长假
			if (arrangeType.getType() == 1) {
				
				ArrangeTypeEditFragment.go(caller.getActivity(), arrangeType.getId(), 0);
				
			} else if (arrangeType.getType() == 3) {
				
				ArrangeTypeEditFragment.go(caller.getActivity(), arrangeType.getId(), 1);
				
			}
		}

	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

		final ArrangeType arrangeType = caller.getList().get(position);

		ViewUtils.confirm(caller.getActivity(), "是否删除？", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {

				AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(caller.getActivity());

				if (tokenKeeper.isLogin()) {

					RequestParams params = new RequestParams();
					params.put("uid", tokenKeeper.getUser().getUid());
					params.put("arrange_type_id", arrangeType.getId());

					ApiRequest.arrangeTypeInfoDel.request(caller, params);

				}

			}

		});

		return false;
	}

}